package org.market.domain.strategy.service.rule.imp;

import lombok.extern.slf4j.Slf4j;
import org.market.domain.strategy.model.entity.RuleActionEntity;
import org.market.domain.strategy.model.entity.RuleMatterEntity;
import org.market.domain.strategy.model.valobj.RuleLogicCheckTypeVO;
import org.market.domain.strategy.repository.IStrategyRepository;
import org.market.domain.strategy.service.annotation.LogicStrategy;
import org.market.domain.strategy.service.rule.ILogicFilter;
import org.market.domain.strategy.service.rule.tree.factory.DefaultLogicFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author: Insight
 * @Description: TODO
 * @Date: 2025/2/20 下午9:57
 * @Version: 1.0
 */

@Slf4j
@Component
@LogicStrategy(logicModel = DefaultLogicFactory.LogicModel.RULE_LOCK)
public class RuleUnlockFilter implements ILogicFilter<RuleActionEntity.DuringRaffleActionEntity> {
    @Resource
    private IStrategyRepository strategyRepository;

    private Long raffleCount = 1L;

    @Override
    public RuleActionEntity<RuleActionEntity.DuringRaffleActionEntity> filter(RuleMatterEntity filterMatterEntity) {
        String userId = filterMatterEntity.getUserId();
        Long strategyId = filterMatterEntity.getStrategyId();
        Integer awardId = filterMatterEntity.getAwardId();

        log.info("测试-抽奖中商品是否上锁目录 userId:{},strategyId:{},awardId:{}",userId,strategyId,awardId);

        String ruleValue =
                strategyRepository.queryStrategyRuleValue(strategyId,awardId,filterMatterEntity.getRuleModel());

        Long count = Long.valueOf(ruleValue);

        if (raffleCount >= count){
            return RuleActionEntity.<RuleActionEntity.DuringRaffleActionEntity>builder()
                    .code(RuleLogicCheckTypeVO.ALLOW.getCode())
                    .info(RuleLogicCheckTypeVO.ALLOW.getInfo())
                    .build();
        }

        return RuleActionEntity.<RuleActionEntity.DuringRaffleActionEntity>builder()
                .code(RuleLogicCheckTypeVO.TAKE_OVER.getCode())
                .info(RuleLogicCheckTypeVO.TAKE_OVER.getInfo())
                .build();
    }
}
